﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Vefskil5.Models;
using System.Net;
using System.IO;
using System.Configuration;

namespace Vefskil5.Controllers
{
    public class SongController : ApiController
    {
        List<AudioBlaster3000> songs = new List<AudioBlaster3000>();

        // GET /api/song
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET /api/song/5
        public string Get(int id)
        {
            IEnumerable<string> songPathList = Directory.GetFiles(ConfigurationManager.AppSettings["upload"]);


            List<TagLib.File> mp3files = new List<TagLib.File>();
            foreach (var item in songPathList)
            {
                mp3files.Add(TagLib.File.Create(item));
            }

            int counter = 0;
            List<AudioBlaster3000> songs = new List<AudioBlaster3000>();
            foreach (var i in mp3files)
            {
                
                songs.Add(new AudioBlaster3000
                {
                    Id = counter,
                    Artist = i.Tag.Performers.ToString(),
                    Title = i.Tag.Title.ToString(),
                    Genre = i.Tag.Genres.ToString(),
                    FileName = i.ToString()
                });
                counter++;
            }

            AudioBlaster3000 song = songs.Find(x => x.Id == id);
            // Create the web request 
            HttpWebRequest request = WebRequest.Create("http://api.guitarparty.com/v2/songs/?query=" + song.Title) as HttpWebRequest;
            request.Headers["Guitarparty-Api-Key"] = "9768d20d26324bf437e5e91a4ac7e9f826568502";

            // Get response 
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream 
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Console application output 
                return reader.ReadToEnd();
            }
        }

        // POST /api/song
        public void Post(string value)
        {
        }

        // PUT /api/song/5
        public void Put(int id, string value)
        {
        }

        // DELETE /api/song/5
        public void Delete(int id)
        {
        }
    }
}
